<?php

namespace app\models;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\DateTime;
use yii\db\Expression;

class StaffRela extends ActiveRecord
{


    public function rules()
    {
        return [

            ['cust_limit', 'integer'],

        ];

    }

    public function getStaffs()
    {
        return $this->hasOne(Staff::className(), ['user_id' => 'sub_user_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }


    // 查询本月成交
    public function getOrderResumes()
    {
        $beginDate = new Expression("DATE_FORMAT('" . date("Y-m",time()) . "-01', '%Y%m%d')");
        $endDate   = new Expression("DATE_FORMAT('" . date('Y-m',strtotime('+1 month') ). "-01', '%Y%m%d')");
        return $this->hasOne(OrderResumes::className(), ['staff_id' => 'sub_user_id'] )->select([  'count(*) as qCount'])->andFilterWhere(['between','create_date',$beginDate, $endDate]);
    }

    // 查询本月新增客源
    public function getOrders()
    {
        $beginDate = new Expression("DATE_FORMAT('" . date("Y-m",time()) . "-01', '%Y%m%d')");
        $endDate   = new Expression("DATE_FORMAT('" . date('Y-m',strtotime('+1 month') ). "-01', '%Y%m%d')");
//        $date = DateTime::createFromFormat('m/d/Y', '10/27/2014');
//        $date->setTime(0,0,0);
      //  $beginDate =  "to_date('" . date("Y-m",time()) . ",'Y-m')" ;
        //$endDate   =  "to_date('" . date('Y-m',strtotime('+1 month'))  . ",'Y-m')" ;
        return $this->hasOne(Order::className(), ['create_staff_id' => 'sub_user_id'] )->select([  'count(*) as qCount'])->andFilterWhere(['between','create_date',$beginDate, $endDate]);
    }

    // 查询本月新增房源
    public function getProds()
    {
//        $beginDate = "DATE_FORMAT('" . date("Y-m",time()) . "-01'), '%Y%m%d')";
//        $endDate   = "DATE_FORMAT('" . date('Y-m',strtotime('+1 month')) . "'-01'), '%Y%m%d')";
        $beginDate = new Expression("DATE_FORMAT('" . date("Y-m",time()) . "-01', '%Y%m%d')");
      //  $beginDate =  "to_date('" . date("Y-m",time()) . ",'Y-m')" ;
        $endDate   = new Expression("DATE_FORMAT('" . date('Y-m',strtotime('+1 month')) . "-01', '%Y%m%d')");
        return $this->hasOne(ProdInst::className(), ['create_staff_id' => 'sub_user_id'] )->select([  'count(*) as qCount'])->andFilterWhere(['between','create_date',$beginDate, $endDate]);
    }

    // 属于自己的订单
    public function getMyOrders()
    {
        $where = [];
        $where['state'] = '30001';
        return $this->hasOne(Order::className(), ['staff_id' => 'sub_user_id'] )->select([  'count(*) as qMyCount'])->where($where);
    }

    // 查询超时订单 未完成 未作废 30001
    public function getOutOrders()
    {

        $where = [];
        $where['state'] = '30001';
        $modifyDate   = new Expression("DATE_FORMAT('" . date('Y-m-d',strtotime('-3 day') ). "', '%Y%m%d')");

        return $this->hasOne(Order::className(), ['staff_id' => 'sub_user_id'] )->select([  'count(*) as qCount'])->where($where)->andFilterWhere(['<=','modify_date',$modifyDate]);
    }

}

?>
